package com.jinke.api.modules.ums.task;

import cn.hutool.core.collection.CollectionUtil;
import com.jinke.api.modules.app.enums.OrderStatus;
import com.jinke.api.modules.app.model.Order;
import com.jinke.api.modules.app.model.OrderParcel;
import com.jinke.api.modules.app.model.UserOrderTask;
import com.jinke.api.modules.app.service.IUserOrderTaskService;
import com.jinke.api.modules.app.service.OrderParcelService;
import com.jinke.api.modules.app.service.OrderService;
import com.jinke.api.modules.third.trackApi.TrackApiUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Profile;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.time.Duration;
import java.time.LocalDateTime;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @author huang
 * @data 2024-06-06
 */
@Slf4j
@Component
@Profile({"prod"})
@RequestMapping("/task")
@RestController
public class UserOrderTasks {
    @Autowired
    private IUserOrderTaskService iUserOrderTaskService;
    @Autowired
    private OrderService orderService;
    @Autowired
    private OrderParcelService orderParcelService;


    /**
     * 注册TrackApi物流信息
     */
    @RequestMapping(value = "/register", method = RequestMethod.GET)
    @Scheduled(cron = "0 42 9 * * ?")
    public void registerTrackApi() {

        //查询昨天一天之内创建的订单
        // 获取当前时间
        LocalDateTime now = LocalDateTime.now();
        // 减去一天的时间
        LocalDateTime oneDayBefore = now.minus(Duration.ofDays(1));
        List<UserOrderTask> orders = iUserOrderTaskService.lambdaQuery().ge(UserOrderTask::getCreateTime, oneDayBefore).lt(UserOrderTask::getCreateTime, now).list();
        log.info("==================JOB注册物流号开始预计条数{}=============",orders.size());
        if (CollectionUtil.isNotEmpty(orders)) {
            //查询是否有对应的订单及订单状态是否处于待运输
            List<Order> orderList = orderService.lambdaQuery()
                    .select(Order::getId, Order::getStatus)
                    .eq(Order::getStatus, OrderStatus.RECENT.getValue())
                    .in(Order::getId, orders.stream().map(UserOrderTask::getOrderId).collect(Collectors.toList()))
                    .list();
            if (CollectionUtil.isNotEmpty(orderList)) {
                //获取运单号，注册
                List<OrderParcel> parcels = orderParcelService.lambdaQuery()
                        .select(OrderParcel::getTrackingNumber)
                        .in(OrderParcel::getOrderId, orderList.stream().map(Order::getId).collect(Collectors.toList()))
                        .list();
                 List<String> number2s=new ArrayList<>();
                Set<String> numbers =new HashSet<>();
                for (OrderParcel parcel : parcels) {
                    if (parcel!=null){

                        String trackingNumber = parcel.getTrackingNumber();
                        if (trackingNumber!=null){
                            numbers.add(trackingNumber);
                        }
                    }
                }
                number2s.addAll(numbers);
//                        = parcels.stream()
//                        .map(OrderParcel::getTrackingNumber)
//                        .filter(tn -> tn!= null)
//                        .collect(Collectors.toList());
                int batchSize = 40;
                for (int i = 0; i < number2s.size(); i += batchSize) {

                    List<String> batch = number2s.subList(i, Math.min(numbers.size(), i + batchSize));
                     TrackApiUtils.register(batch);
                }
            }
        }
    }

    public static void main(String[] args) {
        List<String> batch = Arrays.asList("778105034396","778105099100","778105099110","778105511848");
        //TrackApiUtils.register(batch);

        TrackApiUtils.getTrackinfo(batch);


    }


}
